import store from '../../store'
import { ElMessage, ElMessageBox } from 'element-plus'
import AllStock from '../../assets/AllStock'

export default {
  name: 'Trade',
  data () {
    return {
      type: '买入',
      TradeData: {
        code: 'sh600519',
        price: {},
        number: 0
      },
      usingStock: 'sh600519',
      positionData: [
        {}
      ],
      capitalData: {},
      stockData: {},
      ratio: {},
      availableStockNumber: 0,
      TradeInfo: {
        userid: store.state.user.id
      }
    }
  },
  mounted () {
    if (this.$route.query.stockCode !== undefined) {
      this.TradeData.code = this.$route.query.stockCode
      this.usingStock = this.$route.query.stockCode
      this.type = this.$route.query.tradeType
    }
    store.dispatch('getCapital', store.state.user.id)
      .then(response => {
        this.positionData = response.data.positionData
        this.capitalData = response.data.capitalData
        this.getInstantStock(this.TradeData.code)
      })
      .catch(error => {
        console.log('connect failed')
        console.log(error)
      })
    this.timer = setInterval(() => {
      this.getInstantStock(this.usingStock)
    }, 120000)
  },
  beforeUnmount () {
    if (this.timer) {
      clearInterval(this.timer)
      this.timer = null
    }
  },
  methods: {
    getInstantStock (code) {
      store.dispatch('instantStock', code)
        .then(response => {
          this.usingStock = code
          this.availableStockNumber = 0
          for (var i = 0; i < this.positionData.length; i++) {
            if (this.positionData[i].stockCode === this.TradeData.code) {
              this.availableStockNumber = this.positionData[i].number
              break
            }
          }
          this.stockData = response.data
          this.TradeData.price = this.stockData.现价
          var now = this.stockData.现价
          var yesterday = this.stockData.昨收
          this.ratio = Math.round((parseFloat(now) - parseFloat(yesterday)) / parseFloat(yesterday) * 10000) / 100.00.toFixed(2)
        })
        .catch(error => {
          console.log('connect failed')
          console.log(error)
        })
    },
    judgeCode (code) {
      const allStock = AllStock.data().stockData
      var judge = false
      for (let i = 0; i < allStock.length; ++i) {
        if (allStock[i][0] === code) {
          judge = true
          break
        }
      }
      return judge
    },
    enter () {
      if (!this.judgeCode(this.TradeData.code)) {
        ElMessage.error('请输入正确的股票代码')
        return
      }
      this.getInstantStock(this.TradeData.code)
      console.log('test')
    },
    clickBuy () {
      this.TradeInfo.stockcode = this.usingStock
      this.TradeInfo.stockname = this.stockData.证券简称
      this.TradeInfo.cost = this.stockData.现价
      this.TradeInfo.number = this.TradeData.number
      if (this.TradeData.number * this.stockData.现价 > this.capitalData.可用资金) {
        ElMessage.error('可用资金不足')
      } else if (!this.TradeData.number > 0) {
        ElMessage.warning('请输入购买数量')
      } else {
        this.alertBuy()
      }
    },
    alertBuy () {
      ElMessageBox.confirm(
        '买入' + this.TradeData.number + '股' + this.stockData.证券简称,
        '确认操作',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          center: true
        }
      )
        .then(() => {
          store.dispatch('buyStock', this.TradeInfo)
            .then(response => {
              setTimeout(2000)
              location.reload()
            })
            .catch(error => {
              console.log('connect failed')
              console.log(error)
            })
          ElMessage({
            type: 'success',
            message: '订单提交成功'
          })
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '交易取消'
          })
        })
    },
    clickSell () {
      this.TradeInfo.stockcode = this.usingStock
      this.TradeInfo.stockname = this.stockData.证券简称
      this.TradeInfo.cost = this.stockData.现价
      this.TradeInfo.number = this.TradeData.number
      if (this.TradeData.number > this.availableStockNumber) {
        ElMessage.error('持仓数量不足')
      } else if (!this.TradeData.number > 0) {
        ElMessage.warning('请输入卖出数量')
      } else {
        this.alertSell()
      }
    },
    alertSell () {
      ElMessageBox.confirm(
        '卖出' + this.TradeData.number + '股' + this.stockData.证券简称,
        '确认操作',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          center: true
        }
      )
        .then(() => {
          store.dispatch('sellStock', this.TradeInfo)
            .then(response => {
              setTimeout(2000)
              location.reload()
            })
            .catch(error => {
              console.log('connect failed')
              console.log(error)
            })
          ElMessage({
            type: 'success',
            message: '订单提交成功'
          })
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '交易取消'
          })
        })
    }
  }
}
